package cn.bocweb.spinning.view.forun;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForunGetCatBean;

/**
 * Created by chenchangxing on 16/4/6.
 */
public class ForunSelectCateDialog extends Dialog {

    ForunSendTopicActivity context;
    ForunSelectCateAdapter adapter;
    String cat;
    List<ForunGetCatBean.DataBean> forun;
    String cat_name;
    int comfirmed = 0;
    int selected = 0;

    @Bind(R.id.list)
    RecyclerView list;
    @Bind(R.id.submit)
    Button submit;

    public ForunSelectCateDialog(ForunSendTopicActivity context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_forun_select_cate);


        setTitle("选择社区");
    }

    @Override
    protected void onStart() {
        super.onStart();
        ButterKnife.bind(this);
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        adapter = new ForunSelectCateAdapter(forun);

        manager.setOrientation(LinearLayoutManager.VERTICAL);

        list.setLayoutManager(manager);
        list.setAdapter(adapter);

        cat = forun.get(comfirmed).getId();
        cat_name = forun.get(comfirmed).getTitle();
        adapter.setSelected(comfirmed);
        adapter.notifyDataSetChanged();

        adapter.setOnItemClickListener(new ForunSelectCateAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, String id) {
                cat = id;
                cat_name = forun.get(position).getTitle();
                selected = position;
                adapter.setSelected(position);
                adapter.notifyDataSetChanged();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.setCat(cat, cat_name);
                comfirmed = selected;
                dismiss();
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        ButterKnife.unbind(this);
    }

    public void setForun(List<ForunGetCatBean.DataBean> forun) {
        this.forun = forun;
    }
}
